﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Quanly_dethi.Class;

namespace Quanly_dethi
{
    public partial class Them_Lop_MonThi : Form
    {
        private csB_Lop l;
        private csB_MonThi m;
        int k = 0;
        public Them_Lop_MonThi()
        {
            l = new csB_Lop(bienketnoi.tenmay, bienketnoi.taikhoan, bienketnoi.matkhau, bienketnoi.tencsdl);
            m=new csB_MonThi(bienketnoi.tenmay, bienketnoi.taikhoan, bienketnoi.matkhau, bienketnoi.tencsdl);
            InitializeComponent();
            load();
            loaddd();
            loadcb_lop();
        }
        //Lớp
        private void dr_lop_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int i = e.RowIndex;

                
                k = int.Parse(dr_lop[0, i].Value.ToString());
                txt_tenlop.Text = dr_lop[1, i].Value.ToString();
                
                if (dr_lop[1, i].Value.ToString() == "")
                {
                    MessageBox.Show("Bạn click vào ô không có dữ liệu!!");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void load()
        {
            DataTable viewdata = new DataTable("viewdata");

            DataColumn themcot = viewdata.Columns.Add("Mã Lớp", typeof(int));
            themcot.AllowDBNull = true;
            viewdata.Columns.Add("Tên Lớp", typeof(string));
           
            string tenlop = "";
            for (int i = 0; i < l.select_all().Rows.Count; i++)
            {        
                int ma = int.Parse(l.select_all().Rows[i][0].ToString());
                tenlop = l.select_all().Rows[i][1].ToString();
               
                viewdata.Rows.Add(ma, tenlop);
            }
            dr_lop.DataSource = viewdata;
           
        }
        private void bt_them_Click(object sender, EventArgs e)
        {
            csE_lop ad_moi = new csE_lop();
            ad_moi.Tenlop = txt_tenlop.Text;

            l.Insert(ad_moi);
            load();
        }
        private void bt_sua_Click(object sender, EventArgs e)
        {
            csE_lop ad_sua = new csE_lop();
            ad_sua.Malop = k;
            ad_sua.Tenlop = txt_tenlop.Text;
            //ad_sua.Malop = int.Parse(cb_lop.SelectedValue.ToString());
            // ad_sua.Update(ad_sua);
           // m.Update(ad_sua);
            l.Update(ad_sua);
            load();
            loaddd();
        }
        private void bt_xoa_Click(object sender, EventArgs e)
        {
         //chưa có code nên chưa làm
           
        }






        //Môn Thi
        private void dr_mon_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }
        public void loaddd()
        {
            DataTable viewdata2 = new DataTable("viewdata2");

            DataColumn themcot = viewdata2.Columns.Add("Mã Môn", typeof(int));
            themcot.AllowDBNull = true;
            viewdata2.Columns.Add("Tên Môn", typeof(string));
            viewdata2.Columns.Add("Tên Lớp", typeof(string));
            string tenmon = "",lop = "";
            for (int i = 0; i < m.select_all().Rows.Count; i++)
            {
                int ma = int.Parse(m.select_all().Rows[i][0].ToString());
                tenmon = m.select_all().Rows[i][1].ToString();
                lop = m.select_all().Rows[i][2].ToString();
                viewdata2.Rows.Add(ma, tenmon, lop);
            }
            dr_mon.DataSource = viewdata2;
        }

        private void bt_themmon_Click(object sender, EventArgs e)
        {
            csE_MonThics ad_moi = new csE_MonThics();
            ad_moi.TenMon = txt_tenmon.Text;
           
            ad_moi.Malop = int.Parse(cb_lop.SelectedValue.ToString());
            m.Insert(ad_moi);
            loaddd();
        }
        private void loadcb_lop()
        {
            cb_lop.DataSource = l.SelectArr();
            cb_lop.DisplayMember = "Name";
            cb_lop.ValueMember = "Value";

        }

        private void bt_suamon_Click(object sender, EventArgs e)
        {
            csE_MonThics ad_sua = new csE_MonThics();
            ad_sua.MaMon = k;
            ad_sua.TenMon = txt_tenmon.Text;
            ad_sua.Malop = int.Parse(cb_lop.SelectedValue.ToString());
           // ad_sua.Update(ad_sua);
            m.Update(ad_sua);
            loaddd();
        }

        private void dr_lop_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int i = e.RowIndex;


                k = int.Parse(dr_lop[0, i].Value.ToString());
                txt_tenlop.Text = dr_lop[1, i].Value.ToString();

               // cb_lop.Text = dr_mon[2, 1].Value.ToString();
                if (dr_mon[1, i].Value.ToString() == "")
                {
                    MessageBox.Show("Bạn click vào ô không có dữ liệu!!");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        // nham roi kia, viet 2 cai1  tuong7 tu nhau do9 uk  lam di


        private void dr_mon_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int i = e.RowIndex;


                k = int.Parse(dr_mon[0, i].Value.ToString());
                txt_tenmon.Text = dr_mon[1, i].Value.ToString();

                cb_lop.Text = dr_mon[2, i].Value.ToString();
                if (dr_mon[1, i].Value.ToString() == "")
                {
                    MessageBox.Show("Bạn click vào ô không có dữ liệu!!");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

       
       
        
        //private void bt_sua_Click(object sender, EventArgs e)
        //{
        //    csE_Admin ad_sua = new csE_Admin();
        //    ad_sua.Manguoidung = k;
        //    ad_sua.Tennguoidung = txt_TenND.Text;
        //    ad_sua.Matkhau = txt_matkhau.Text;
        //    ad_sua.Maquyen = int.Parse(Cb_Quyen.SelectedValue.ToString());
        //    ad_sua.Xoa = 0;
        //    ad.Update(ad_sua);
        //    load();
        //}
       
    }
}
       